﻿// ===============================================================================
// XCI.NET 西安交通信息WinForm开发框架 2018 版权所有
// ===============================================================================
using System;
using System.Data;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using XCI.Core;

namespace XCI.Helper
{
    /// <summary>
    /// 身份证验证帮助类
    /// </summary>
    public class IdCardHelper
    {
        private static readonly DataTable dt_IdType;


        private static readonly string[] ls_jy = { "1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2" };
        private static readonly int[] li_quan = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 };

        static IdCardHelper()
        {
            dt_IdType = new DataTable();
            dt_IdType.Columns.Add("text");
            dt_IdType.Columns.Add("value");
            dt_IdType.Rows.Add(new object[] { "居民身份证", "A" });
            dt_IdType.Rows.Add(new object[] { "军官证", "C" });
            dt_IdType.Rows.Add(new object[] { "士兵证", "D" });
            dt_IdType.Rows.Add(new object[] { "军官离退休证", "E" });
            dt_IdType.Rows.Add(new object[] { "境外人员身份证明", "F" });
            dt_IdType.Rows.Add(new object[] { "外交人员身份证明", "G" });
        }

        /// <summary>
        /// 绑定身份证类别的名称
        /// </summary>
        /// <param name="cb">The cb.</param>
        public static void InitIdType(ComboBox cb)
        {
            cb.DataSource = dt_IdType;
            cb.DisplayMember = "text";
            cb.ValueMember = "value";
            cb.DropDownStyle = ComboBoxStyle.DropDownList;
        }

        /// <summary>
        /// 获取身份证类别的名称
        /// </summary>
        /// <returns></returns>
        public static DataTable CreateIDType()
        {
            return dt_IdType;
        }

        /// <summary>
        /// 验证身份证结果
        /// </summary>
        /// <param name="idcard">身份证号码</param>
        /// <returns>正确的时候返回string.Empty</returns>
        public static BoolResult Validate(string idcard)
        {
            if (idcard.Length != 18 && idcard.Length != 15)
            {
                return new BoolResult(false, "身份证号码必须是15或者18位。");
            }

            Regex rg = new Regex(@"^\d{17}(\d|X)$");
            if (!rg.Match(idcard).Success)
            {
                return new BoolResult(false, "身份证号码必须为数字或者X。");
            }
            if (idcard.Length == 15)
            {
                idcard = IdCard15To18(idcard);
            }
            else if (idcard.Length == 18)
            {
                int ll_sum = 0;
                for (int i = 0; i < 17; i++)
                {
                    var tmp = int.Parse(idcard.Substring(i, 1));
                    ll_sum += tmp * li_quan[i];
                }
                ll_sum %= 11;
                if (idcard.Substring(17, 1) != ls_jy[ll_sum])
                {
                    return new BoolResult(false, "身份证号码最后一位应该是" + ls_jy[ll_sum] + "。");
                }
            }


            try
            {
                DateTime.Parse(idcard.Substring(6, 4) + "-" + idcard.Substring(10, 2) + "-" + idcard.Substring(12, 2));
            }
            catch
            {
                return new BoolResult(false, "非法生日。");
            }
            return BoolResult.True;
        }

        /// <summary>
        /// 15位身份证明号码转化成18位用来编码
        /// </summary>
        /// <param name="idcard">15位的身份证号码</param>
        /// <returns></returns>
        public static string IdCard15To18(string idcard)
        {
            /*
              
             string ls_jy[] =  { "1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"}, t, ls_sfzmhm
integer li_quan[] =  { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1}
int ll_sum = 0, i
 
ls_sfzmhm = mid(sfzmhm, 1, 6) + '19' + mid(sfzmhm, 7)
for i = 1 to len(ls_sfzmhm)
    ll_sum += integer(mid(ls_sfzmhm, i, 1)) * li_quan[i]
next
 
ll_sum = mod(ll_sum, 11)
 
ls_sfzmhm += ls_jy[ll_sum + 1]
 
return ls_sfzmhm
             */

            if (idcard == null || idcard.Length != 15)
            {
                return idcard;
            }
            else
            {
                int ll_sum = 0;
                string result = idcard.Substring(0, 6) + "19" + idcard.Substring(6, 9);
                for (int i = 0; i < 17; i++)
                {
                    var tmp = int.Parse(result.Substring(i, 1));
                    ll_sum += tmp * li_quan[i];
                }
                ll_sum %= 11;
                result += ls_jy[ll_sum];
                return result;
            }
        }

        /// <summary>
        /// 获取身份证对应省份的区划
        /// </summary>
        /// <param name="id">身份证</param>
        /// <returns>头两位+4个0</returns>
        public static string GetProvince(string id)
        {
            return id.Substring(0, 2) + "0000";
        }

        /// <summary>
        /// 获取身份证对应县市的区划
        /// </summary>
        /// <param name="id">身份证</param>
        /// <returns>头4位+2个0</returns>
        public static string GetCity(string id)
        {
            return id.Substring(0, 4) + "00";
        }

        /// <summary>
        /// 获取身份证对应地区的区划
        /// </summary>
        /// <param name="id">身份证</param>
        /// <returns>头6位</returns>
        public static string GetArea(string id)
        {
            return id.Substring(0, 6);
        }

        /// <summary>
        /// 根据身份证判断是否男女
        /// </summary>
        /// <param name="id">身份证号码</param>
        /// <returns>返回"男"或者"女"</returns>
        public static string GetSexName(string id)
        {
            int sexStr;
            if (id.Length == 15)
            {
                sexStr = Convert.ToInt32(id.Substring(14, 1));
            }
            else if (id.Length == 18)
            {
                sexStr = Convert.ToInt32(id.Substring(16, 1));
            }
            else
            {
                return "";
            }
            return sexStr % 2 == 0 ? "女" : "男";
        }

        /// <summary>
        /// 根据身份证获取出生年月
        /// </summary>
        /// <param name="id">身份证号码</param>
        /// <returns>出生年月</returns>
        public static string GetBirthday(string id)
        {
            string result;
            if (id.Length == 15)
            {
                result = "19" + id.Substring(6, 2) + "-" + id.Substring(8, 2) + "-" + id.Substring(10, 2);
            }
            else if (id.Length == 18)
            {
                result = id.Substring(6, 4) + "-" + id.Substring(10, 2) + "-" + id.Substring(12, 2);
            }
            else
            {
                return "";
            }
            return result;
        }

        /// <summary>
        /// 根据出生日期，计算精确的年龄
        /// </summary>
        /// <param name="birthDate">生日</param>
        public static int GetAge(string birthDay)
        {
            DateTime birthDate = DateTime.Parse(birthDay);
            DateTime nowDateTime = DateTime.Now;
            int age = nowDateTime.Year - birthDate.Year;
            //再考虑月、天的因素
            if (nowDateTime.Month < birthDate.Month ||
                (nowDateTime.Month == birthDate.Month && nowDateTime.Day < birthDate.Day))
            {
                age--;
            }
            return age;
        }
    }
}